package itsm.isperp.module.controller.project;

import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.request.JqGridFilterRule;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.project.ProjProject;
import itsm.isperp.module.entity.project.ProjProjectMember;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.module.service.project.ProjProjectMemberService;
import itsm.isperp.module.service.project.ProjProjectService;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 项目人员管理控制器
 * 
 * @author lizx
 * @date 2013-2-26
 * @version V1.0
 * @date 2013-10-7 增加listProjectByUsername方法
 * 
 */
@Controller
@RequestMapping("project/member")
public class ProjProjectMemberController extends
		BaseController<ProjProjectMember> {

	@Autowired
	protected ProjProjectService projProjectService;

	@Autowired
	protected ProjProjectMemberService projProjectMemberService;

	@Autowired
	protected AppRoleService appRoleService;

	@Override
	public ProjProjectMemberService getService() {
		return this.projProjectMemberService;
	}

	@Override
	public String getPageTitle() {
		return "项目人员管理";
	}

	/**
	 * 编辑界面
	 * 
	 * @param id
	 *            唯一标识
	 * @return 编辑视图
	 */
	@Override
	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) {
		String projectId = req.getParameter("projectId");

		ModelAndView mv = new ModelAndView();
		ProjProjectMember model = this.getService().findOne(id);
		if (model == null) {
			model = new ProjProjectMember();
			model.setId(HibernateUUIDGenerator.generate());
			model.setProjProjectId(projectId);

			ProjProject project = projProjectService.findOne(projectId);
			if (project != null) {
				model.setPlanInDate(project.getStartDate());
				model.setPlanOutDate(project.getEndDate());
			}
		}

		mv.addObject("pageTitle", this.getPageTitle());
		// 职位列表

		mv.addObject("positionList",
				this.appRoleService.findAllRole("id", "name"));

		mv.addObject("model", model);
		return mv;
	}

	/**
	 * 项目人员列表
	 * 
	 * @param projectId
	 *            项目Id
	 * @param request
	 *            请求
	 * @return 人员json数据
	 */
	@RequestMapping("projectMemberList")
	@ResponseBody
	public PageResponse<Map<String, Object>> projectMemberList(
			String projectId, JqGridRequest request) {

		// 增加项目id字段过滤规则
		request.addFilterRule(new JqGridFilterRule("proj_project_id",
				JqGridFilterRuleOp.eq, projectId));

		return this.projProjectMemberService.findAllDetailByRequest(request);

	}

	/**
	 * 获取项目人员列表
	 * 
	 * @param projectId
	 *            项目Id
	 * @return 项目的人员
	 */
	@RequestMapping("projectMemberUserList")
	@ResponseBody
	public List<Map<String, Object>> projectMemberUserList(String projectId) {

		return this.projProjectService.findAllMemberByProjectId(projectId);

	}

	/**
	 * 根据用户的帐号获取用户具有的项目
	 * 
	 * @param username
	 *            用户帐号
	 * @return 用户关联的项目
	 */
	@RequestMapping("listProjectByUsername")
	@ResponseBody
	public List<ProjProject> listProjectByUsername(String username) {
		return this.projProjectMemberService.findMemberProjects(username);

	}

	@RequestMapping("save")
	@ResponseBody
	@Override
	public ResultMessage save(ProjProjectMember model, HttpServletRequest req) {
		boolean b = this.getService().userExistsInProject(
				model.getAppUserName(), model.getProjProjectId());
		if (b) {
			if (this.getService().exists(model.getId())) {
				this.getService().saveOrUpdate(model, req);
			} else {
				return ResultMessage.failure("此人员已存在项目人力计划中。");
			}
		}

		this.getService().saveOrUpdate(model, req);
		return ResultMessage.newInstance(true, null, model.getId());

	}
	
	@RequestMapping("saveProjectMember")
  @ResponseBody
  public ResultMessage saveProjectMember(String projectId, String userName) {
    if(userName.indexOf(",")>-1)
    {
       String[] names=userName.split(",");
       for(String n:names)
       {
         this.getService().saveProjectMember(projectId,
              n);
       }
       
    }else
    {
       this.getService().saveProjectMember(projectId,
          userName);
    }
    return ResultMessage.newInstance(true);
  }
	
}
